from enum import Enum
from typing import List

from fastapi import APIRouter, HTTPException, Query

from app.schema import ResponseSchema
from app.service.school_service import SchoolInfoService

router = APIRouter(prefix="/school", tags=['school'])


@router.get("/get_school_info/{school_id}", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_news_by_id(school_id: int):
    result = await SchoolInfoService.get_schoolInfo_by_id(school_id)
    if result:
        return ResponseSchema(detail="success", result=result)
    else:
        raise HTTPException(status_code=404, detail="School not found !")


@router.get("/get_all_schools", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_all_schools(
        is_985: bool = Query(False, description="Filter by 985 status"),
        is_211: bool = Query(False, description="Filter by 211 status"),
        is_dfc: bool = Query(False, description="Filter by dfc status"),
        is_imp_country: bool = Query(False, description="Filter by imp_country status"),
        filter_cities: List[str] = Query(None, description="Filter by cities"),
        school_name: str = Query(None, description="Filter by school name")
):
    result = await SchoolInfoService.find_schools_by_filters(is_985, is_211, is_dfc, is_imp_country, filter_cities,
                                                             school_name)
    return ResponseSchema(detail="success", result=result)


class RankTypeEnum(str, Enum):
    rkRank = "rkRank"
    wslRank = "wslRank"
    xyhRank = "xyhRank"


@router.get("/get_school_rank", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_all_schools(rank_type: RankTypeEnum = Query(..., description="Rank type")):
    if rank_type != "rkRank" and rank_type != "wslRank" and rank_type != "xyhRank":
        raise HTTPException(status_code=400, detail={"rank_type": rank_type})
    result = await SchoolInfoService.find_school_rank(rank_type)
    if result:
        return ResponseSchema(detail="success", result=result)
    else:
        raise HTTPException(status_code=404, detail="error")


@router.get("/get_all_city", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_all_city():
    result = await SchoolInfoService.find_all_province()
    if result:
        return ResponseSchema(detail="success", result=result)
    else:
        raise HTTPException(status_code=404, detail="error")


@router.get("/get_all_brochure/{school_id}", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_all_brochure(school_id: int):
    brochures = await SchoolInfoService.get_titles_by_school(school_id)
    return ResponseSchema(detail="success", result=brochures)


@router.get("/get_brochure_content/{brochure_id}", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_brochure_content(brochure_id: int):
    content = await SchoolInfoService.get_content_by_id(brochure_id)

    if content:
        return ResponseSchema(detail="success", result=content)
    else:
        raise HTTPException(status_code=404, detail="School Brochure not found")


@router.get("/get_plans/{school_id}", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_plans(
        school_id: str,
        nature: str = Query(None, description="Plan nature"),
):
    content = await SchoolInfoService.get_plans_by_nature(school_id, nature)

    if content:
        return ResponseSchema(detail="success", result=content)
    else:
        raise HTTPException(status_code=404, detail="School Brochure not found")

@router.get("/get_score/{school_id}", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_plans(
        school_id: int,
):
    content = await SchoolInfoService.get_score_by_id(school_id)

    if content:
        return ResponseSchema(detail="success", result=content)
    else:
        raise HTTPException(status_code=404, detail="School not found")